//
//  LLT_VideoViewController_LXH.m
//  CoderLLT
//
//  Created by lanou3g on 15/12/11.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "LLT_VideoViewController_LXH.h"
#import "LLT_LiveBigTableViewCell_LXH.h"
#import "LLT_LiveSmallTableViewCell_LXH.h"
#import "LLT_VideoTableViewCell_LXH.h"
#import "AFNetworking.h"
#import "LLT_PersonModel.h"
#import "LLT_LiveModel.h"
#import "LLT_LiveViewController_LXH.h"
#import "LLT_VideoDetailTableViewController_LXH.h"
#import "LLT_NavigationViewController_LXM.h"
#import "MJRefresh.h"
#import "DirectionMenubarView.h"
#import "LLT_DataSender_LXH.h"


#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface LLT_VideoViewController_LXH ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>


@property (weak, nonatomic) IBOutlet UIView *dicrectionMenu;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UITableView *liveTableView;
@property (weak, nonatomic) IBOutlet UITableView *videoTableView;

@property (nonatomic, strong) NSMutableArray *liveArray;

@property (nonatomic, strong) NSMutableDictionary *personDictionary;

@property (nonatomic, strong) NSMutableArray *personArray;

//刷新
@property (nonatomic, strong) MJRefreshNormalHeader *refreshHeader;

@property (nonatomic, strong) MJRefreshBackNormalFooter *refreshFooter;

@property (strong, nonatomic) DirectionMenubarView *dicm;

@property (nonatomic, strong) LLT_DataSender_LXH *dataSender;

@end

@implementation LLT_VideoViewController_LXH


#pragma mark --- dealloc
-(void)dealloc
{
    self.liveTableView.delegate = nil;
    self.liveTableView.dataSource = nil;
    self.videoTableView.dataSource = nil;
    self.videoTableView.delegate = nil;
    self.scrollView.delegate = nil;
    self.liveArray = nil;
    self.personArray = nil;
    self.personDictionary = nil;
    self.refreshFooter = nil;
    self.refreshHeader = nil;
    self.dicm = nil;
    self.dataSender = nil;
}



/**
 * 设置横竖屏
 */


-(BOOL)shouldAutorotate {
    return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}



/**
 * 电池栏 文字变白
 */
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark --- 懒加载

/**
 * 懒加载
 */
-(NSMutableArray *)liveArray{
    
    if (!_liveArray) {
        _liveArray = [NSMutableArray new];
    }
    return _liveArray;
}

-(NSMutableDictionary *)personDictionary{
    
    if (!_personDictionary) {
        _personDictionary = [NSMutableDictionary new];
    }
    return _personDictionary;
}

-(NSMutableArray *)personArray{
    
    if (!_personArray) {
        _personArray = [NSMutableArray new];
    }
    return _personArray;
}

#pragma mark --- 视图加载完毕

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.dataSender = [[LLT_DataSender_LXH alloc]init];
    
    [self makeLiveData];
    
    [self makePersonData];
    
    [self makeTableView];
    
    [self makescrollView];
    
    [self makeDic];

}

-(void)viewWillAppear:(BOOL)animated{
    
    CGFloat max = MAX(SCREEN_HEIGHT, SCREEN_WIDTH);
    CGFloat min = MIN(SCREEN_HEIGHT, SCREEN_WIDTH);
    self.view.frame = CGRectMake(0, 0, min, max);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置view里面的各种视图

-(void)makeDic
{
    self.dicm = [DirectionMenubarView instntceWithFrame:CGRectMake(0,0,WIDTH,35) andTitleArr:(@[@"直播",@"视频"].mutableCopy)];
    __weak typeof(self) __weakSelf = self;
    self.dicm.doChang = ^{
        __weakSelf.scrollView.contentOffset = CGPointMake(([__weakSelf.dicm getTapedIndex]*WIDTH),__weakSelf.scrollView.frame.origin.y);
    };
    [self.dicrectionMenu addSubview:self.dicm];
}


/**
 * 点击跳转下载页面
 */
- (IBAction)didClickDownloadButton:(UIBarButtonItem *)sender {
    
    //NSLog(@"调到下载页面");
}

/**
 * 获取直播页面的数据
 */
-(void)makeLiveData{
    
    __weak typeof(self.liveTableView) myLiveView = self.liveTableView;
    
    __weak typeof(self) weakSelf = self;
    
    [self.dataSender getLiveArrayByCount:self.liveArray.count];
    
    self.dataSender.sendLiveArray = ^(NSMutableArray *liveArray)
    {
        [weakSelf.liveArray addObjectsFromArray:liveArray];
        
        [myLiveView reloadData];
    };
    
 }

/**
 * 获取视频页面的数据
 */
-(void)makePersonData{
    
    __weak typeof(self.videoTableView) myTableView = self.videoTableView;
    
    __weak typeof(self) weakSelf = self;
    
    [self.dataSender getPersonArrayByCount:weakSelf.personArray.count];
    
    self.dataSender.sendPersonArray = ^(NSMutableArray *personArray)
    {
        [weakSelf.personArray addObjectsFromArray:personArray];
        
        [myTableView reloadData];
    };
    
    self.dataSender.sendPersonDictionary = ^(NSMutableDictionary *personDictionary)
    {
        [weakSelf.personDictionary addEntriesFromDictionary:personDictionary];
        
        [myTableView reloadData];
    };
    
    
}


/**
 * 设置两个tableView的属性
 */
-(void)makeTableView{
    [self.liveTableView addSubview:self.refreshHeader];
    [self.liveTableView addSubview:self.refreshFooter];
    
    self.liveTableView.delegate = self;
    self.liveTableView.dataSource = self;
    self.videoTableView.dataSource = self;
    self.videoTableView.delegate = self;

    [self.liveTableView registerNib:[UINib nibWithNibName:@"LLT_LiveBigTableViewCell_LXH" bundle:nil] forCellReuseIdentifier:@"BigCell"];
    [self.liveTableView registerNib:[UINib nibWithNibName:@"LLT_LiveSmallTableViewCell_LXH" bundle:nil] forCellReuseIdentifier:@"SmallCell"];
    [self.videoTableView registerNib:[UINib nibWithNibName:@"LLT_VideoTableViewCell_LXH" bundle:nil] forCellReuseIdentifier:@"personCell"];

}



/**
 * 设置scrollView的属性
 */
-(void)makescrollView{
    
    self.scrollView.delegate = self;

}

#pragma mark - 实现segment与scrollView同步


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if(scrollView.tag == 444){
        int count = scrollView.contentOffset.x/CGRectGetWidth(self.view.frame);
        
        [self.dicm changTitleWithIndex:count];
    }
    
}


-(void)changeAction:(UISegmentedControl *)sender{
    
    switch (sender.selectedSegmentIndex) {
        case 0:
            self.scrollView.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
        default:
            break;
    }
    
}

#pragma mark - 设置tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    if (tableView.tag == 111) {
    
        return 1;
        
    }else{
        
        return self.personDictionary.count;
    }

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView.tag == 111) {
    
        return self.liveArray.count;
        
    }else {
        
        NSString *key = self.personArray[section];
        NSArray *array = self.personDictionary[key];
        return array.count;
        
    }
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag == 111) {

        if (indexPath.row == 0) {
            //直播的大cell
          return [self makeLiveBigCellWithTableView:tableView AtIndexPath:indexPath];
         }else{
            //直播的小cell
          return [self makeLiveSmallCellWithTableView:tableView AtIndexPath:indexPath];
        }
    }else {
        
        //视频的cell
          return [self makePersonCellWithTableView:tableView AtIndexPath:indexPath];
    }
    
}

/**
 * 设置大cell
 */
-(LLT_LiveBigTableViewCell_LXH *)makeLiveBigCellWithTableView:(UITableView *)tableView AtIndexPath:(NSIndexPath *)indexPath{
    
    LLT_LiveBigTableViewCell_LXH *cell = [tableView dequeueReusableCellWithIdentifier:@"BigCell" forIndexPath:indexPath];
    LLT_LiveModel *live = self.liveArray[indexPath.row];
    cell.liveModel = live;
    return cell;
}

/**
 * 设置小cell
 */
-(LLT_LiveSmallTableViewCell_LXH *)makeLiveSmallCellWithTableView:(UITableView *)tableView AtIndexPath:(NSIndexPath *)indexPath{
    
    LLT_LiveSmallTableViewCell_LXH *cell = [tableView dequeueReusableCellWithIdentifier:@"SmallCell" forIndexPath:indexPath];
    LLT_LiveModel *live = self.liveArray[indexPath.row];
    cell.liveModel = live;
    return cell;
}

/**
 * 设置视频的cell
 */
-(LLT_VideoTableViewCell_LXH *)makePersonCellWithTableView:(UITableView *)tableView AtIndexPath:(NSIndexPath *)indexPath{
    
    LLT_VideoTableViewCell_LXH *cell = [tableView dequeueReusableCellWithIdentifier:@"personCell" forIndexPath:indexPath];
    NSString *key = self.personArray[indexPath.section];
    NSArray *array = self.personDictionary[key];
    
    LLT_PersonModel *person = array[indexPath.row];
    cell.personModel = person;

    return cell;
}

/**
 * 设置每个cell 的高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag == 111) {
        
        if (!indexPath.row) {
            return 200;
        }else{
            return 100;
        }
        
    }else{
        
        return 70;
        
    }
    
}

/**
 * 设置标题
 */
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(15, 0, self.view.frame.size.width - 15, 20)];
    UILabel *label = [[UILabel alloc]initWithFrame:view.frame];
    label.font = [UIFont systemFontOfSize:11];
    [view addSubview:label];
    if (tableView.tag == 222) {
        label.text = self.personArray[section];
    }else{
        label.text = @"直播列表";
    }
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 20;
}

/**
 * 跳转页面
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == 222) {
            NSString *key = self.personArray[indexPath.section];
            NSArray *arr =self.personDictionary[key];
            LLT_PersonModel *person = arr[indexPath.row];
            LLT_VideoDetailTableViewController_LXH *l = [[LLT_VideoDetailTableViewController_LXH alloc]init];
            l.dm_uid = person.dm_link;
            LLT_NavigationViewController_LXM *n = [[LLT_NavigationViewController_LXM alloc]initWithRootViewController:l];
            [self presentViewController:n animated:YES completion:nil];
            return;
        }
    
    if (tableView.tag == 111) {
        LLT_LiveModel *liveModel = self.liveArray[indexPath.row];
        LLT_LiveViewController_LXH *liveController = [MainStoryBoard instantiateViewControllerWithIdentifier:@"liveVC"];
        liveController.url = liveModel.url_info[@"url"];
        [self presentViewController:liveController animated:YES completion:nil];
        return;
    }
    
    return;
}


#pragma mark - set MJRefresh

/**
 * 下来刷新
 */
-(MJRefreshNormalHeader *)refreshHeader{
    
    if (!_refreshHeader) {
        
        _refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            self.liveArray = [NSMutableArray new];
            
            [self makeLiveData];
            
            [self.liveTableView reloadData];
            
            [_refreshHeader endRefreshing];
            
        }];
    }
    
    return _refreshHeader;
    
}

/**
 *上拉加载
 */
-(MJRefreshBackNormalFooter *)refreshFooter{
    
    if (!_refreshFooter) {
        
        _refreshFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            
            [self makeLiveData];
            
            [_refreshFooter endRefreshing];
            
        }];
    }
    
    return _refreshFooter;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
